<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="90"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { saveBeijing } from '@/api/potenCus/customer/potenCusManage';
import { dictionary } from '@/utils/dictMixin';
import { IDNumber } from '@/utils/validate'

export default {
  name: 'arcfox',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.potentialCustomersId = this.rowData.potentialCustomerId;
      this.formList.forEach(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'customerType',
          placeholder: '请选择',
          itemList: this.createDictList('1523'),
          rules: [{ required: true, message: '请选择客户类型', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'ctCode',
          placeholder: '请选择',
          itemList: this.createDictList('1508'),
          rules: [{ required: true, message: '请选择证件类型', trigger: 'change' }],
          change: val => {
            console.log(val)
            if(val==15081001){  //身份证
              this.formList.find(item => item.fieldName ==='certificateNo').rules=[{ required: true, trigger: 'blur', validator: IDNumber }]
            }
            if(val==15081002){   //护照
              this.formList.find(item => item.fieldName ==='certificateNo').maxlength = 9
              this.formList.find(item => item.fieldName ==='certificateNo').rules=[{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                  if (value === undefined || value === '' || value === null) {
                    callback()
                  }
                  const g = /^[0-9a-zA-Z]{1,9}$/
                  if (!g.test(value)) {
                    callback(new Error('请输入正确的护照号码'))
                  }
                  callback()
                }
              }]
            }
            if(val==15081003){   //统一社会信用代码
              this.formList.find(item => item.fieldName ==='certificateNo').maxlength = 18
              this.formList.find(item => item.fieldName ==='certificateNo').rules=[{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                  if (value === undefined || value === '' || value === null) {
                    callback()
                  }
                  const g = /^[0-9A-Z]{1,18}$/
                  if (!g.test(value)) {
                    callback(new Error('请输入18位统一社会信用代码'))
                  }
                  callback()
                }
              }]
            }
          }
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'certificateNo',
          placeholder: '请输入',
          maxlength: '',
          itemList: [],
          rules: []
        },

        // {
        //   type: 'INPUT',
        //   label: '证件号码',
        //   fieldName: 'certificateNo',
        //   placeholder: '请输入',
        //   itemList: [],
        //   rules: [{ required: true, message: '请输入证件号码',trigger: 'blur' },
        //           { required: true, trigger: 'blur', validator: IDNumber }]
        // },
      ]
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log('this.saveData', this.saveData)
      if (this.saveData !== null) {
         if (null == this.saveData.writeCode) {
          this.saveData.writeCode = '';
        }
        this.saveData.potentialCustomersId = this.potentialCustomersId
        let res = await saveBeijing(this.saveData )
        console.log('res', res)
        if (res.resultCode === 200) {
          this.closeDialog('refresh')
          this.$message.success('下单成功')
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>

<style lang="less" scoped>
</style>
